package com.ruoyi.project.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.tool.upload.service.UploadService;
import com.ruoyi.project.travel.domain.Scenic;
import com.ruoyi.project.travel.domain.ScenicStat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.travel.domain.Advertising;
import com.ruoyi.project.travel.service.IAdvertisingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 广告信息 Controller
 *
 * @author HYD
 * @date 2020-06-14
 */
@RestController
@RequestMapping("/travel/advertising" )
public class AdvertisingController extends BaseController {
    @Autowired
    private IAdvertisingService advertisingService;

    @Autowired
    private UploadService uploadService;

    /**
     * 查询广告信息 列表
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(Advertising advertising) {
        startPage();
        QueryWrapper<Advertising> queryWrapper = new QueryWrapper<Advertising>();
        if(advertising.getType() != null){
            queryWrapper.eq("type",advertising.getType());
        }
        if(advertising.getCreateTime() != null){
            queryWrapper.eq("create_time",advertising.getCreateTime());
        }
        List<Advertising> list = advertisingService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出广告信息 列表
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:export')" )
    @Log(title = "广告信息 " , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Advertising advertising) {
        QueryWrapper<Advertising> queryWrapper = new QueryWrapper<Advertising>(advertising);
        List<Advertising> list = advertisingService.list(queryWrapper);
        ExcelUtil<Advertising> util = new ExcelUtil<Advertising>(Advertising. class);
        return util.exportExcel(list, "advertising" );
    }

    /**
     * 获取广告信息 详细信息
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(advertisingService.getOne(new QueryWrapper<Advertising>().eq("id",id)));
    }

    /**
     * 新增广告信息 
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:add')" )
    @Log(title = "广告信息 " , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(Advertising advertising,
                          @RequestParam(value = "mainImgs",required = false) MultipartFile mainImgs) {
        if(mainImgs!=null) advertising.setUrl(uploadService.uploadImage(mainImgs,"scenic"));
        advertising.setCreateTime(new Date());
        return toAjax(advertisingService.save(advertising) ? 1 : 0);
    }

    /**
     * 修改广告信息 
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:edit')" )
    @Log(title = "广告信息 " , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(Advertising advertising,
                           @RequestParam(value = "mainImgs",required = false) MultipartFile mainImgs) {
        if(mainImgs != null){
            advertising.setUrl(uploadService.uploadImage(mainImgs,"scenic"));
        }
        return toAjax(advertisingService.updateById(advertising) ? 1 : 0);
    }

    /**
     * 删除广告信息 
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:remove')" )
    @Log(title = "广告信息 " , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id: ids) {
            Advertising advertising = advertisingService.getById(id);
            uploadService.delImage(advertising.getUrl());
        }
        return toAjax(advertisingService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 删除图片
     * @param id
     * @param url 删除图片的路径
     * @return
     */
    @PreAuthorize("@ss.hasPermi('travel:advertising:remove')" )
    @Log(title = "删除景区图片" , businessType = BusinessType.DELETE)
    @ResponseBody
    @RequestMapping("/delImg")
    public AjaxResult delImg(@RequestBody Map<String,Object> map){
        Advertising advertising = new Advertising();
        advertising.setId(Long.parseLong(String.valueOf(map.get("id"))));
        advertising.setUrl("");
        uploadService.delImage(String.valueOf(map.get("url")));
        return toAjax(advertisingService.updateById(advertising) ? 1 : 0);
    }

}
